/*
 * BasicTsMediaCaster.h
 *
 *  Created on: 2019年2月28日
 *      Author: terry
 */

#ifndef BASICTSMEDIACASTER_H_
#define BASICTSMEDIACASTER_H_

#include "TsMediaCaster.h"
#include "TsMuxer.h"
#include "TsCaster.h"

#include "MediaPacketQueue.h"
#include "H264PropDetector.h"
#include "NaluJoiner.h"
#include "NetAddress.h"


namespace av
{

typedef std::deque< NetAddress >        NetAddressArray;
typedef std::shared_ptr< TsMuxer >      TsMuxerPtr;


class BasicTsMediaCaster: public TsMediaCaster, public TsMuxerSink
{
public:
    BasicTsMediaCaster();
    virtual ~BasicTsMediaCaster();

    virtual void setID(int id);

    virtual int getID();

    virtual bool addTarget(const std::string& ip, uint16_t port);

    virtual void removeTarget(const std::string& ip, uint16_t port);

    virtual size_t getTargetCount();

    virtual void removeAllTarget();

    virtual bool write(int type, uint8_t* data, int size, int64_t pts, int duration, int flags);

    virtual void setName(const char* name);

protected:
    bool openMuxer(const MediaFormat& fmt);
    void closeMuxer();

    void fireEvent(CasterEvent& event);
    void fireEvent(int type);

protected:
    comn::CriticalSection   m_cs;
    int     m_id;

    TsMuxerPtr  m_muxer;
    MediaFormat m_format;

    int64_t m_videoLastPts;
    int64_t m_audioLastPts;

    H264PropDetector    m_propDetector;
    std::shared_ptr< NaluJoiner >   m_naluJoiner;

    NetAddressArray     m_targets;
    std::string m_name;

};


} /* namespace av */

#endif /* BASICTSMEDIACASTER_H_ */
